package cn.net.gemai.springboot_activemq.controller;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderController {

    /**
     * 消息发送
     */
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 下单Demo
     * 点对点模式
     */
    @GetMapping("addOrder")
    public Object addOrder() {
        /**
         * 下单并更新库存
         */
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("库存队列1");
        jmsMessagingTemplate.convertAndSend(activeMQQueue, "商品ID:  111, 数量：2");

        Map<String, Object> result = new HashMap<>();
        result.put("result", "添加订单成功1");
        return result;
    }

    /**
     * 下单Demo
     * 发布\订阅模式
     */
    @GetMapping("addOrder2")
    public Object addOrder2() {
        /**
         * 下单并更新库存
         */
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("库存通知1");
        jmsMessagingTemplate.convertAndSend(activeMQTopic, "通知内容是：商品ID:  111, 数量：2");

        Map<String, Object> result = new HashMap<>();
        result.put("result", "添加订单成功2");
        return result;
    }

}
